package codingPractice.huawei;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Test3 {
    static class LogFile {
        int processId;
        int size;
        int downloadCount;

        public LogFile(int processId, int size, int downloadCount) {
            this.processId = processId;
            this.size = size;
            this.downloadCount = downloadCount;
        }
    }

    public static int maxDownloads(int N, int M, int C, List<LogFile> logFiles) {
        List<List<LogFile>> processLogs = new ArrayList<>();
        for(int i=0;i<N;i++) {
            processLogs.add(new ArrayList<>());
        }
        for(LogFile log : logFiles) {
            processLogs.get(log.processId).add(log);
        }

        int totalSize = 0;
        int totalDownloadCount = 0;

        List<LogFile> optionLogs = new ArrayList<>();

        boolean[] selectedProcess = new boolean[N];

        for(int i=0;i<logFiles.size();i++) {
            LogFile log = logFiles.get(i);
            int processId = log.processId;

            if (!selectedProcess[processId]) {
                selectedProcess[processId] = true;
                totalSize += log.size;
                totalDownloadCount += log.downloadCount;

                if(totalSize > C) {
                    return -1;
                } else {
                    optionLogs.add(log);
                }
            }

            optionLogs.sort((a,b) -> b.downloadCount - a.downloadCount);


        }


        optionLogs.sort((a,b) -> b.downloadCount - a.downloadCount);

        for(LogFile log : optionLogs) {
            if (totalSize + log.size <= C) {
                totalSize += log.size;
                totalDownloadCount += log.downloadCount;
            } else {
                break;
            }
        }
        return totalDownloadCount;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()) {
            int N = sc.nextInt();
            int M = sc.nextInt();
            int C = sc.nextInt();

            List<LogFile> logs = new ArrayList<>();
            for(int i=0;i<N*M;i++) {
                int processId = sc.nextInt();
                int size = sc.nextInt();
                int downloads = sc.nextInt();
                logs.add(new LogFile(processId, size, downloads));
            }

            int result = maxDownloads(N, M, C, logs);
            System.out.println(result);

        }
    }
}
